package reviewAlgorithms.binarySearch;

public class FirstBadVersion {
    public static void main(String[] args) {
        System.out.println(firstBadVersion(5));
    }
    public static int firstBadVersion(int n) {
        if (n < 1){
            return -1;
        }
        int left = 1;
        int right = n;
        int center;
        while (left < right){
            center = (left + right)/2;
            if (isBadVersion(center)){
                if (center + 1 <= n){
                    if (!isBadVersion(center + 1)){
                        return center + 1;

                    }else {
                        left = center + 1;
                    }
                }else {
                    return -1;
                }
            }else {
                if (center - 1 >= 0){
                    if (isBadVersion(center - 1)){
                        return center;
                    }else {
                        right = center - 1;
                    }
                }else {
                    return -1;
                }
            }
        }
        return -1;
    }

    private static boolean isBadVersion(int center) {
        if (center >= 2){
            return false;
        }
        return true;
    }
}
